package net.zjitc.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import net.zjitc.domain.User;
import net.zjitc.service.UserService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by Administrator on 2017/2/27.
 */
@Controller
@Scope("prototype")
public class UserAction extends ActionSupport implements ModelDriven<User>{
    /*定义一个log变量*/
    private Log log= LogFactory.getLog(this.getClass());
    private String message;
    private String username;
    private String password;
    private User model=new User();
    @Resource
    private UserService userService;

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

/*    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }*/
    /*登录界面*/

    /**
     * 登录界面，返回登录界面视图
     * @return
     */
    public String loginUI(){
        return "loginUI";
    }


    /*登录请求*/
    public String login(){
        //如果有用户的话，返回登录成功
        User user=userService.findByUsernamePassword(model.getUsername(),model.getPassword());

        if (user!=null){
/*            message="登录成功";*/
            log.debug("登录成功！");
            log.info("登录成功！欢迎。。。。");

            this.addActionMessage("登录成功");
            List<User> userList=userService.findAll();
            /*讲查询出来的对象放在值栈空间中*/
            ActionContext.getContext().put("userList",userList);
            ActionContext.getContext().getSession().put("user",user);
            return "loginsuccess";
        }
        //否则返回登录失败
/*        message="用户名或密码错误";*/
        this.addActionError("用户名或密码错误");
        return "loginFail";
    }
    /**
     * 注册界面，返回注册界面视图
     * @return
     */
    public String registUI(){
        return "registUI";
    }
    /*注册请求*/
    public String regist(){
        //如果没有有用户的话，返回注册成功
        User user=userService.findByzcUsernamePassword(model.getUsername(),model.getPassword());

        if (user==null){
            message="注册成功";

            /*查询所有用户*/


            return "registsuccess";
        }
        //否则返回登录失败
        message="次用户已存在";
        return "registFail";
    }

    @Override
    public User getModel() {
        return model;
    }
}
